<?php

namespace App\Http\Controllers\NonPublicVersion;


use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Order as Model;
use App\Http\Controllers\Common;
use Illuminate\Support\Facades\DB;


class Order extends Controller
{
    use Common;
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $orderno = $request->input('orderno' ,'');
        $keywords = $request->input('keywords' ,'');
        $status = $request->input('status' ,'');
        $pageSize = $request->get('paginate',config('web.page.num'));
        $query = [];
        $queryParams = [
            'orderno' => $orderno,
            'keywords' => $keywords,
            'status' => $status,
        ];

        $where = [];
        $orWhere = [];
        if ($orderno){
            $where[] = ['order.orderno', 'like', "%{$orderno}%"];
        }

        if ($keywords){
            $where[] = ['user.username', 'like', "%{$keywords}%"];
            /*$orWhere[] = ['user.username', 'like', "%{$keywords}%"];
            $orWhere[] = ['game_config.app_name', 'like', "%{$keywords}%"];
            $orWhere[] = ['user.phone', 'like', "%{$keywords}%"];*/
        }
        if ($status){
            $where[] = ['order.status', $status];
        }
        DB::enableQueryLog();
        $lists = DB::table('order')->select('order.*', 'user.username', 'user.phone','game_config.app_name')
            ->leftJoin('user', 'user.uid', '=', 'order.uid')
            ->leftJoin('game_config', 'game_config.game_id', '=', 'order.app_id')
            ->where($where)
            ->orWhere($orWhere)
            ->orderBy('order.id', 'DESC')
            ->paginate($pageSize);
        dump(DB::getQueryLog());
        /*$lists = new Model();
        if (!empty($where)){
            $lists = $lists->where($where)->orderBy('id','DESC')->paginate($pageSize);
        } else{
            $lists = $lists::orderBy('id','DESC')->paginate($pageSize);
        }*/
        //dd($lists);
        return view('nonPublicVersion.order.index',[
            'lists' => $lists,
            'params' => http_build_query($query),
            'query' => $queryParams
        ]);
    }
}
